package com.wtwd.device.controller;


import com.wtwd.common.sendemail.service.SendEmailService;
import com.wtwd.device.model.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2021-11-24
 */
@Api(tags = "问题反馈")
@RestController
@RequestMapping("/problem-feedback")
public class ProblemFeedbackController {

    @Value("${email.problem.user}")
    private String sendUser;


    @Resource
    private SendEmailService sendEmailService;

    /**
     * 添加问题反馈(此功能占时不同步数据库,web平台做管理时在扩充)
     */

    @ApiOperation(value = "提交问题反馈", notes = "上传头像后,返回头像访问路径")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "submitterMail", value = "提交人邮箱", required = true, dataType = "String"),
            @ApiImplicitParam(name = "problemDescription", value = "问题内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "files", value = "问题反馈文件", required = true, dataTypeClass = MultipartFile[].class),
            @ApiImplicitParam(name = "appName", value = "app名称", required = true, dataType = "String"),
    })
    @PostMapping("/submitProblemDescription")
    public ResultVO addProblemFeedback(@RequestParam(value = "submitterMail") String submitterMail,
                                       @RequestParam(value = "problemDescription") String problemDescription,
                                       @RequestParam(value = "files") MultipartFile[] files,
                                       @RequestParam(value = "appName") String appName
    ) {
        List<File> filesList = new ArrayList<>();
        if (files != null && files.length > 0) {
            for (MultipartFile problemImage : files) {
                try {
                    File file = MultipartFileToFile(problemImage);
                    if (file == null) {
                        continue;
                    }
                    filesList.add(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        String content = "<dev><h4>提交人邮箱:</h4><p>" + submitterMail + "</p>" +
                "<h4>反馈类容:</h4><p>" + problemDescription + "</p></dev>";
        boolean isSuccess = sendEmailService.sendWithFiles(sendUser, content, appName + " " + "用户反馈", filesList);
        if (isSuccess) {
            return ResultVO.success();
        }
        return ResultVO.error();
    }


    /**
     * 用临时文件转换
     *
     * @param multiFile
     * @return
     */
    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        File file = null;
        try {
            file = File.createTempFile(System.currentTimeMillis() + "", prefix);
            multiFile.transferTo(file);
            file.deleteOnExit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }


}

